<template>
    <main-wrap ref="mianEle" class="collection" :loading="true" :load="onLoad" :end="page.finished" :outOnScroll="onscroll">
        <template v-slot:header> 
                <Header title="所有商品"/>
                <div class="ctopBox">
                    <my-search v-model="keyword" placeholder="搜索艺术品" @search="getSearchData"/>
                    <goods-type @change="getTabData"/>
                    <Sort @clickHandle="sortData"/>
                </div>
        </template>
        <water-fall :lists='page.lists'/>
        
        <!-- <ul class="shopLst">
            <li v-for="dt in page.lists" :key="dt.goodsId">
                <a @click="goDetail(dt)">
                    <div class="imgbbox">
                        <Image :url="dt.imageUrl"/>
                    </div>
                    <div class="listTxt">
                        <h4>
                            <div class="title">{{dt.title}}</div>
                            <span @click.stop="addLive(dt)">
                                <i v-if="dt.myLive" class="iconfont icon-zanxuanzhong"></i>
                                <i v-else class="iconfont icon-zan"></i> 
                                {{dt.attentionCount}}
                            </span>
                        </h4>
                        <div class="author">作者：<span>{{dt.artistNick}}</span> <auth-icon/></div>
                        <div class="introduce">{{dt.subtitle}}</div>
                        <div class="price">¥<em>{{dt.internalPrice.decimalTwoString}}</em></div>
                    </div>
                </a>
            </li>
        </ul> -->
    </main-wrap>
</template>

<script>
import {reactive,toRefs, onMounted,computed,getCurrentInstance } from 'vue'
import { useStore } from 'vuex'
import {useRouter} from 'vue-router'
import {toast} from '@/utils/common'
import Header from '@/components/header.vue'
import mainWrap from '@/components/mainWrap.vue'
import Sort from './components/sort.vue'
import goodsType from '@/components/goodsType.vue'
import mySearch from '@/components/mySearch.vue'
import Image from '@/components/image.vue'
import authIcon from '@/components/authIcon.vue'
import waterFall from '@/components/waterfall.vue'
export default {
    components:{
        mainWrap,
        Header,
        Sort,
        mySearch,
        Image,
        goodsType,
        authIcon,
        waterFall
    },
    setup(){
        const store = useStore()
        const router = useRouter()
        const {proxy} = getCurrentInstance()

        const state = reactive({
            statusBarHeight:computed(()=>store.state.arApi.statusBarHeight),
            keyword:'',
            goodsCategoryId:'',
            page:{
                lists:[],
                pageIndex:1,
                pageSize:20,
                finished: false
            },
            sort:{
                orderField: 1,
                orderType: "desc"
            },
            st:''
        })

        onMounted(()=>{
           onLoad()
        })

        const onLoad = ()=> {
            state.st && clearTimeout(state.st)
            // 此函数必须返回一个Promise对象
            return new Promise((resolve, reject) => {
                state.st = setTimeout(async () => {
                    const res = await store.dispatch("arApi/get_collection_list", {
                        page: state.page.pageIndex,
                        limit: state.page.pageSize,
                        keyWord: state.keyword,
                        goodsCategoryId:state.goodsCategoryId,
                        ...state.sort, 
                    });
                    state.page.lists = [...state.page.lists, ...res.data];
                    state.page.pageIndex++;
                    if (state.page.lists.length >= res.total) {
                        state.page.finished = true;
                    }
                    resolve(true);
                }, 500);
            });
        }

        const resetScroll = async()=>{
            proxy.$refs.mianEle.stopScroll()
            state.page.lists=[]
            state.page.pageIndex=1
            state.page.finished=false
            await onLoad()
            proxy.$refs.mianEle.initScroll()
        }

        const sortData = async(obj) =>{
            state.sort = obj
            resetScroll()
        }
        const getSearchData = async(keyword)=>{
            //state.keyword = keyword
            resetScroll()
        }

        const goDetail = (o)=>{
            router.push(`/detail/${o.goodsId}`)
        } 

        const addLive = async (item)=>{
            const {goodsId} = item
            if(item.myLive){  // 取消喜欢
                const res = await store.dispatch("arApi/cancel_goods_live", {goodsId})
                if(res.success){
                    //toast('取消喜欢成功')
                    item.myLive = false
                    item.attentionCount--
                }else{
                    //toast(res.message)
                }
            }else{  // 添加喜欢
                const res = await store.dispatch("arApi/set_goods_live", {goodsId})
                if(res.success){
                    //toast('成功添加喜欢！')
                    item.attentionCount++
                    item.myLive = true
                }else{
                   // toast(res.message)
                }
            }   
        }
        const getTabData = (tid)=>{
            state.goodsCategoryId = tid
            resetScroll()
        }

        const onscroll = top =>{
            const searchEle = document.querySelector('.searchBox')  // 搜索
            const provEle = document.querySelector('.goodsCtgBox') // 头部显示分类div
            if(top > (provEle.offsetHeight + searchEle.offsetHeight)){
                provEle.style.display = 'none'
                searchEle.style.display = 'none'
            }else{
                provEle.style.display = 'block'
                searchEle.style.display = 'flex'
            }
        }

        return {
            ...toRefs(state),
            onLoad,
            sortData,
            getSearchData,
            goDetail,
            addLive,
            getTabData,
            onscroll
        }
    }
}
</script>

<style lang="scss" scoped>
    .collection{
        background-image: url('/images/clbg.png');
        background-position:left top;
        background-repeat: no-repeat;
        background-size:100% auto ;
    }
    .ctopBox{
        padding: 20px 40px 0 40px;
    }
    .shopLst{
        list-style: none;
        padding: 40px;
        padding-bottom: 0;
        margin: 0;
        li{   
            width: 100%;
            margin: 0 0 30px 0;
            padding: 0 0 30px 0;
            border-bottom: 1px solid rgba(255, 255, 255, 0.2);;
            a{
                display: flex;
                max-height: 255px;
                width: 100%;
                padding: 0;
                .imgbbox{
                    display: block;
                    width: 140px;
                    height: 140px;
                }
                .listTxt{
                    flex: 1;
                    padding: 0 0 0 20px;
                    h4{
                        display: flex;
                        font-size: 28px;
                        height: 40px;
                        line-height: 40px;
                        overflow:hidden;
                        .title{
                            flex: 1;
                            overflow:hidden;
                            text-overflow: ellipsis;
                            white-space: nowrap;
                            width: 0;
                            font-weight: bold;
                            padding: 0 16px 0 0;
                            font-size: 28px;
                            font-weight: 500;
                        }
                        span{
                            display: flex;
                            justify-content:end;
                            align-items: center;
                            flex-shrink: 0;
                            font-size: 26px;
                            color: rgba(255, 255, 255, 0.5);
                            i{
                                padding: 0 4px 0 0 ;
                            }
                        }
                    }
                    .author{
                        display: flex;
                        height: 44px;
                        line-height: 44px;
                        font-size: 26px;
                        color: #829393;
                        margin: 13px 0 0 0;
                        width: 100%;
                        overflow:hidden;
                        span{
                            padding: 0 10px 0 0;
                        }
                    }
                    .introduce{
                        max-height: 72px;
                        font-size: 26px;
                        font-weight: 400;
                        color: rgba(255, 255, 255, 0.5);
                        line-height: 36px;
                        margin: 10px 0 0 0;
                        overflow : hidden;
                        text-overflow: ellipsis;
                        display: -webkit-box;
                        -webkit-line-clamp: 2;
                        -webkit-box-orient: vertical;
                    }
                    .price{
                        height: 36px;
                        line-height: 36px;
                        font-size: 26px;
                        color: var(--baseColor);
                        font-weight: 500;
                        margin: 15px 0 0 0;
                        text-align: left;
                        em{
                            font-style: normal;
                            font-size: 34px;
                            font-weight: 500;
                            padding: 0 0 0 4px;
                        }
                    }
                }
            }
            
        }
    }
</style>